package com.mingmen.supplier.activity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;
import com.mingmen.supplier.R;
import com.mingmen.supplier.adapter.NoticeListAdapter;
import com.mingmen.supplier.base.BaseBean;
import com.mingmen.supplier.defineview.ClearEditText;
import com.mingmen.supplier.http.MyOkhttp;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.AppConfig;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.imageloader.GlideImageLoader;
import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.model.PicSendModel;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.SharePreferenceUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;

public class NoticeSendActivity extends AppCompatActivity {

    //返回按钮
    @BindView(R.id.text_return)
    TextView textReturn;


    @BindView(R.id.btn_login)
    Button btn_login;
    @BindView(R.id.title)
    ClearEditText title;
    private ImagePicker imagePicker;
    @BindView(R.id.picture)
    ImageView picture;
    @BindView(R.id.content)
    EditText content;
    @BindView(R.id.linear_return)
    LinearLayout linear_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noticesend);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        createImagePicker();
        token = (String) SharePreferenceUtil.get(this, "token", "");
    }


    @OnClick({R.id.text_return, R.id.btn_login, R.id.picture, R.id.linear_return})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_return:
            case R.id.linear_return:
                finish();
                break;
            case R.id.btn_login:
                if (TextUtils.isEmpty(title.getText().toString())) {
                    Toast.makeText(this, "请输入公告标题", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(file_str)) {
                    Toast.makeText(this, "请上传公告图片", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(content.getText().toString())) {
                    Toast.makeText(this, "请输入公告内容", Toast.LENGTH_SHORT).show();
                } else {
                    HttpResponse.addAnnouncement(token, title.getText().toString(), file_str, content.getText().toString(), new StringCallback(this) {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            BaseBean baseBean = new BaseBean();
                            baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                            if (baseBean.getCode()==0){
                                EventBus.getDefault().post(17);
                                finish();
                            }
                            Toast.makeText(NoticeSendActivity.this, baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                break;
            case R.id.picture:

                Intent intent = new Intent(this, ImageGridActivity.class);
                intent.putExtra(ImageGridActivity.EXTRAS_IMAGES, images);
                //ImagePicker.getInstance().setSelectedImages(images);
                startActivityForResult(intent, 100);
                break;
        }
    }


    public void createImagePicker() {
        imagePicker = ImagePicker.getInstance();

        imagePicker.setImageLoader(new GlideImageLoader());
        imagePicker.setMultiMode(false);
        imagePicker.setShowCamera(true);
        imagePicker.setCrop(false);
        imagePicker.setSaveRectangle(true);
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);
        Integer width = Integer.valueOf(280);
        Integer height = Integer.valueOf(280);
        width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, getResources().getDisplayMetrics());
        height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, getResources().getDisplayMetrics());
        imagePicker.setFocusWidth(width);
        imagePicker.setFocusHeight(height);
        imagePicker.setOutPutX(Integer.valueOf(800));
        imagePicker.setOutPutY(Integer.valueOf(800));
    }


    ArrayList<ImageItem> images = null;

    String path;

    List<File> myfile = new ArrayList<>();
    File file;
    String token;
    String file_str;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == 100) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                for (int i = 0; i < images.size(); i++) {
                    Log.d("dkdkdkk", images.get(i).path);
                    path = images.get(i).path;
                    file = new File(path);
                    myfile.add(file);
                }
                File file = new File(path);
//                File file = new File(path);
                Log.d("Myfile", myfile.toString());
//.params("file", multple)
                for (int i = 0; i < myfile.size(); i++) {
                    File multple = myfile.get(i);
                }

//                MyOkhttp.post("http://192.168.3.18:9001/system/upload/img")
                MyOkhttp.post(AppConfig.IMGURL+"/system/upload/img")
                        .params("token", token)
                        .addFileParams("file", myfile)
                        .execute(new StringCallback(this) {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                Log.d("DKkdkdkd", s);
                                PicSendModel picSendModel = new PicSendModel();
                                picSendModel = GsonUtil.GsonToBean(s, PicSendModel.class);
                                file_str = picSendModel.getData().get(0);
                                myfile.clear();

                            }
                        });
                Bitmap bm = BitmapFactory.decodeFile(images.get(0).path);
                picture.setImageBitmap(bm);
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }
    public void onEventMainThread(Integer integer) {


    }
}




